﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Shapes;



namespace Task_Network_Center
{
    /// <summary>
    /// Logique d'interaction pour FrmAddTask.xaml
    /// </summary>
    public partial class FrmAddTask : Window
    {
        public string CurrentProject;
        public object[,] TempDepartementArray;
        public object[,] TempEmployeArray;
        public object[,] TempPrioriteArray;
        public object[,] TempEquipeArray;

        public FrmAddTask()
        {
            InitializeComponent();
        }

        private void cmdAdd_Click(object sender, RoutedEventArgs e)
        {
            if (txtProjets.Text != "" && txtDescription.Text != "" && cboExecutant.SelectedIndex != -1 && cboInscritPar.SelectedIndex != -1)
            {
                try
                {
                    GCGDataBaseTableLib.TaskTable TTask = new GCGDataBaseTableLib.TaskTable();
                    decimal duree = Convert.ToDecimal(txtDuree.Text);
                    decimal progression = Convert.ToDecimal(txtProgression.Text);
                    TTask.InsertRow(txtProjets, txtDescription, cboInscritPar, datePickerInscription, txtSolution,
                        cboExecutant, txtDuree, txtProgression, txtAvancement, cboEquipe, cboPriorite, datePickerEcheance,
                        cboDepartement, datePickerCompleteLe, cboComplete, txtNote1, txtNote2, txtNote3, txtNote4, TempDepartementArray, TempEmployeArray, TempEquipeArray, TempPrioriteArray);

                   
                    if (chkSendEmail.IsChecked == true)
                    {
                        string executantEmail = "";
                        for (int i = 0; i < TempEmployeArray.GetLength(1); i++)
                        {
                            if (TempEmployeArray[1,i].ToString() == cboExecutant.Text)
                            {
                                executantEmail = TempEmployeArray[2, i].ToString();
                                break;
                            }
                        }
                        if (executantEmail != "")
                        {
                            System.Net.Mail.MailMessage message = new System.Net.Mail.MailMessage();
                            message.To.Add(executantEmail);
                            message.Subject = txtProjets.Text + " - GCG Nouvelle Tâche";
                            message.From = new System.Net.Mail.MailAddress("info@genik.qc.ca");
                            message.Body = "Projet: " + txtProjets.Text + "\nDescription: " + txtDescription.Text;
                            System.Net.Mail.SmtpClient smtp = new System.Net.Mail.SmtpClient("mail.genik.qc.ca", 1025);
                            smtp.Credentials = new System.Net.NetworkCredential("info.genik.qc.ca", "40392gen");
                            smtp.Send(message);
                        }
                        else
                        {
                            MessageBox.Show("L'exécutant que vous avez choisi n'a pas d'adresse courrielle configurée.");
                        }
                    }


                    this.Close();
                }
                catch (Exception)
                {
                    MessageBox.Show("Veuiller entrer une durée ou une progression valide...");

                }
            }
            else
            {
                MessageBox.Show("La tâche doit avoir aux minimum: Projets, Inscrit Par, Description et un Exécutant");
            }

        }

        private void updateCbo()
        {
            for (int i = 0; i < TempEmployeArray.GetLength(1); i++)
            {
                cboInscritPar.Items.Add(TempEmployeArray[1, i].ToString());
                cboExecutant.Items.Add(TempEmployeArray[1, i].ToString());
                cboComplete.Items.Add(TempEmployeArray[1, i].ToString());
            }

            for (int i = 0; i < TempEquipeArray.GetLength(1); i++)
            {
                cboEquipe.Items.Add(TempEquipeArray[1, i].ToString());
            }

            for (int i = 0; i < TempDepartementArray.GetLength(1); i++)
            {
                cboDepartement.Items.Add(TempDepartementArray[1, i].ToString());
            }

            for (int i = 0; i < TempPrioriteArray.GetLength(1); i++)
            {
                cboPriorite.Items.Add(TempPrioriteArray[1, i].ToString());
            }
        }

        private void Window_Loaded_1(object sender, RoutedEventArgs e)
        {
            updateCbo();
            cboComplete.SelectedIndex = -1;
            cboDepartement.SelectedIndex = -1;
            cboEquipe.SelectedIndex = -1;
            cboExecutant.SelectedIndex = -1;
            txtProjets.Text = CurrentProject;
            for (int i = 0; i < TempEmployeArray.GetLength(1); i++)
            {
                if (Convert.ToInt32(TempEmployeArray[0,i]) == GCGGeneralLib.GeneralVar.initialID)
                {
                    cboInscritPar.SelectedIndex = Convert.ToInt32(TempEmployeArray[3, i]);
                    break;
                }
            }
            cboPriorite.SelectedIndex = -1;
            txtDuree.Text = "0";
            txtProgression.Text = "0";
            datePickerInscription.Text = DateTime.Today.ToShortDateString();
            txtProjets.Focus();
        }

        private void cmdCancel_Click(object sender, RoutedEventArgs e)
        {
            this.Close();
        }

        private void lblClearEquipe_MouseEnter(object sender, MouseEventArgs e)
        {
            lblClearEquipe.Foreground = Brushes.Red;
        }

        private void lblClearEquipe_MouseLeave(object sender, MouseEventArgs e)
        {
            lblClearEquipe.Foreground = Brushes.Black;
        }

        private void lblClearPriorite_MouseEnter(object sender, MouseEventArgs e)
        {
            lblClearPriorite.Foreground = Brushes.Red;
        }

        private void lblClearPriorite_MouseLeave(object sender, MouseEventArgs e)
        {
            lblClearPriorite.Foreground = Brushes.Black;
        }

        private void lblClearEcheance_MouseEnter(object sender, MouseEventArgs e)
        {
            lblClearEcheance.Foreground = Brushes.Red;
        }

        private void lblClearEcheance_MouseLeave(object sender, MouseEventArgs e)
        {
            lblClearEcheance.Foreground = Brushes.Black;
        }

        private void lblClearDepartement_MouseEnter(object sender, MouseEventArgs e)
        {
            lblClearDepartement.Foreground = Brushes.Red;
        }

        private void lblClearDepartement_MouseLeave(object sender, MouseEventArgs e)
        {
            lblClearDepartement.Foreground = Brushes.Black;
        }

        private void lblClearCompleteLe_MouseEnter(object sender, MouseEventArgs e)
        {
            lblClearCompleteLe.Foreground = Brushes.Red;
        }

        private void lblClearCompleteLe_MouseLeave(object sender, MouseEventArgs e)
        {
            lblClearCompleteLe.Foreground = Brushes.Black;
        }

        private void lblClearCompletePar_MouseEnter(object sender, MouseEventArgs e)
        {
            lblClearCompletePar.Foreground = Brushes.Red;
        }

        private void lblClearCompletePar_MouseLeave(object sender, MouseEventArgs e)
        {
            lblClearCompletePar.Foreground = Brushes.Black;
        }

        private void lblClearEquipe_MouseUp(object sender, MouseButtonEventArgs e)
        {
            cboEquipe.SelectedIndex = -1;
        }

        private void lblClearPriorite_MouseUp(object sender, MouseButtonEventArgs e)
        {
            cboPriorite.SelectedIndex = -1;
        }

        private void lblClearEcheance_MouseUp(object sender, MouseButtonEventArgs e)
        {
            datePickerEcheance.SelectedDate = null;
        }

        private void lblClearDepartement_MouseUp(object sender, MouseButtonEventArgs e)
        {
            cboDepartement.SelectedIndex = -1;
        }

        private void lblClearCompleteLe_MouseUp(object sender, MouseButtonEventArgs e)
        {
            datePickerCompleteLe.SelectedDate = null;
        }

        private void lblClearCompletePar_MouseUp(object sender, MouseButtonEventArgs e)
        {
            cboComplete.SelectedIndex = -1;
        }
    }
}
